home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / CC_C / 0151.ZIP / WSPM.C < prev    next >
C/C++ Source or Header  |  1985-02-09  |  3KB  |  140 lines

  1. #include </lib/ascii.h>
  2.  
  3. /* Convert WordStar files to Mate. Removes hard returns. */
  4.  
  5. char ibuff[2000];
  6. char obuff[2000];
  7. int ifile,ofile,in,on;
  8.  
  9. main(argc,argv)
  10. int argc;
  11. char *argv[];
  12. {
  13.  
  14. int i,state;
  15. char name[80],c,lastc;
  16.  
  17.     printf("WordStar to Pmate Converter\r\n");
  18.     if (argc < 2) {
  19.         printf("What file?\r\n");
  20.         exit(1);
  21.     }
  22.  
  23.     cpyarg(name,argv[1]);
  24.     ifile= _xopen(name,0);
  25.     if (ifile == -1) {
  26.         printf("Cannot find %s\r\n",name);
  27.         exit(2);
  28.     }
  29.     ofile= _xcreate("wspm.$$$",1);
  30.     if (ofile == -1) {
  31.         printf("Cannot create work file wspm.$$$\r\n");
  32.         exit(2);
  33.     }
  34.  
  35.     state= 0;
  36.     on= 0;
  37.  
  38.     while (in= _xread(ifile,ibuff,sizeof(ibuff))) {
  39.         for (i= 0; i < in; i++) {
  40.             c= ibuff[i];
  41.  
  42.             if (c >= 127) switch(c) {
  43.                 case CR + 128: c= CR; break;
  44.  
  45.                 case LF + 128:
  46.                 case DEL:
  47.                 case 224: c= '\0'; break;
  48.  
  49.                 default: c &= 0x7f; break;
  50.  
  51.  
  52.             } else if (c < ' ') switch (c) {
  53.  
  54.                 case CR: c= CR; break;
  55.  
  56.                 case TAB: c= TAB; break;
  57.  
  58.                 default: c= '\0'; break;
  59.             }
  60.  
  61. /* State machine for stripping CRs and left margins. We exchange soft
  62. CRs for hard ones unless we get two CRs in a row. We toss spaces after
  63. soft CRs. */
  64.             switch (state) {
  65.  
  66.         /* Last char was an ordinary character */
  67.  
  68.             case 0: if (c == '\0') break;
  69.  
  70.                 if (c == CR) state= 1; 
  71.                 else putc(c);
  72.                 break;
  73.  
  74.         /* Last character was a CR */
  75.  
  76.             case 1: if (c == '\0') break;
  77.  
  78.                 if (c == CR) {
  79.                     putc(CR);    /* two in a row */
  80.                     putc(LF);    /* write them out */
  81.                     putc(CR);    /* two in a row */
  82.                     putc(LF);    /* write them out */
  83.                     state= 0;
  84.                     break;
  85.  
  86.                 } else {
  87.                     putc(CR + 128);    /* replace w/ soft */
  88.                     putc(LF);    /* return, */
  89.                     state= 2;    /* fall through ... */
  90.                 }
  91.  
  92.         /* last character was a soft CR */
  93.  
  94.             case 2: if (c == '\0') break;
  95.  
  96.                 if (c == CR) {
  97.                     putc(CR);
  98.                     putc(LF);
  99.                     state= 1;
  100.  
  101.                 } else if ((c == ' ') || (c == TAB)) break;
  102.                 else {
  103.                     putc(c);
  104.                     state= 0;
  105.                 }
  106.                 break;
  107.             }
  108.         }
  109.     }
  110.     _xclose(ifile);
  111.     putc(SUB);
  112.     oflush();
  113.     _xclose(ofile);
  114.     _xdelete("wspm.bak");
  115.     _xrename(name,"wspm.bak");
  116.     _xrename("wspm.$$$",name);
  117.     exit(0);
  118. }
  119.  
  120. /* Put a character into the buffer; if its full, flush to disk. */
  121.  
  122. putc(c)
  123. char c;
  124. {
  125.     if (on >= sizeof(obuff)) oflush();
  126.     obuff[on++]= c;
  127. }
  128. /* Flush the output buffer. */
  129.  
  130. oflush() {
  131.  
  132.     if (_xwrite(ofile,obuff,on) != on) {
  133.         printf("Disk full!\r\n");
  134.         _xclose(ofile);
  135.         _xclose(ifile);
  136.         exit(1);
  137.     }
  138.     on= 0;
  139. }
  140.